package com.example.demo.util;

import org.apache.pdfbox.pdmodel.PDDocument;

import java.io.File;

/**
 * Created by zyc(378661553@qq.com) on 2021/10/19 13:48
 */
public class Pdf {
    public static void main(String[] args) throws Exception {
        String     pdfPath       = "D:\\Software\\Tencent\\WeChatData\\WeChatData\\WeChat Files\\wxid_g7ri3vmqxpt022\\FileStorage\\File\\2021-10\\四川无人机样板间建设相关资料\\四川无人机样板间建设相关资料";
        PDDocument doc           = PDDocument.load(new File(pdfPath + "/四川无人机样板间建设涉及业务中台服务调用服务规范.pdf"));
        int        numberOfPages = doc.getNumberOfPages();
        System.out.println(numberOfPages);
        int count = 1;
        for (int i = 0; i < numberOfPages; i += 5) {
            int startIndex = i;
            int endIndex   = ((i + 4) >= 137 ? 136 : (i + 4));
            System.out.println(startIndex + "====" + endIndex);

            File file = new File(pdfPath + String.format("/新建文件夹/四川无人机样板间建设涉及业务中台服务调用服务规范-%d.pdf", count++));
            if (!file.exists()){
                boolean newFile = file.createNewFile();
                if (!newFile){
                    throw new RuntimeException("创建文件失败" + file.toString());
                }
            }
            PDDocument temp = new PDDocument();
            for (int j = startIndex; j <= endIndex; j++) {
                temp.addPage(doc.getPage(j));
            }
            temp.save(file);
            temp.close();
        }
        doc.close();
    }
}
